The invention relates to a virtual machine, and more particularly to a virtual machine which supports reinitialisation of classes.
Programs written in the Java programming language (Java is a trademark of Sun Microsystems Inc) are generally run in a virtual machine environment, rather than directly on hardware. Thus a Java program is typically compiled into byte-code form, and then interpreted by the Java virtual machine (JVM) into hardware commands for the platform on which the JVM is executing. The JVM itself is an application running on the underlying operating system. An important advantage of this approach is that Java applications can run on a very wide range of platforms, providing of course that a JVM is available for each platform.
Java is an object-oriented language. Thus a Java program is formed from a set of class files having methods that represent sequences of instructions (somewhat akin to subroutines). A hierarchy of classes can be defined, with each class inheriting properties (including methods) from those classes which are above it in the hierarchy. For any given class in the hierarchy, its descendants (i.e. below it) are call subclasses, whilst its ancestors (i.e. above it) are called superclasses. At run-time objects are created as instantiations of these class files, and indeed the class files themselves are effectively loaded as objects. One Java object can call a method in another Java object. In recent years Java has become very popular, and is described in many books, for example xe2x80x9cExploring Javaxe2x80x9d by Niemeyer and Peck, O""Reilly and Associates, 1996, USA, and xe2x80x9cThe Java Virtual Machine Specificationxe2x80x9d by Lindholm and Yellin, Addison-Wedley, 1997, USA.
It is desirable to run the same application repeatedly on a JVM, avoiding the need to reload the application classes at each new start. This is particularly the case in a server environment, where an application may represent a transaction (e.g. for a database), and the system has to process many transactions in rapid succession. One difficulty with this is that each application expects to start on a fresh, clean, platform to ensure predictable results. However, the JVM specification states that initialization will be performed once and only once for any given class. The prior art does not disclose any satisfactory method of running an application repeatedly and reliably on a single JVM without the overhead of reloading the application classes.
Accordingly, the invention provides a method of operating a computer system including a virtual machine supporting an object-oriented environment, in which programs to run on the virtual machine are formed from classes loaded into the virtual machine by a class loader, wherein a class must be initialized before being used by a program, said method comprising the steps of:
starting a first application on the virtual machine, including loading and initializing a set of one or more classes for said first application;
running said first application;
after the first application has finished running, resetting at least one class from said set of one or more classes from the first application; and
starting a second application on the virtual machine, said second application initializing said at least one class that has been reset from the first application, prior to use of said at least one class by the second application.
With this approach a new application can be started in a clean state, without having to load all the application classes as required in the prior art. Rather, the classes from a previous application can be retained in the system, and reinitialized before being used by the new application. This results in a significant performance improvement in terms of the start-up time of the new application, since it does not have the overhead of having to reload application classes. Clearly for this approach to be of significant benefit, the first and second applications must either be the same application (possibly running with different input data), or else similar or related applications which share a number of classes.
The invention is especially advantageous in a transaction processing environment. Here an application represents a transaction, and the system is used to process a large number of successive transactions, which are typically all performing variations of the same basic operation. In this case the duration of any individual transaction is likely to be relatively short, and so the ability to reuse classes and so reduce the transaction start up time is consequently of particular importance.
In the preferred embodiment each class has an initialization flag which is set when the class is initialized. The step of resetting at least one class therefore involves resetting the initialization flag to its state prior to initialization. This makes the class look as if it has not yet been initialized, and will therefore prompt its (re)initialisation by the second application. The step of resetting further includes setting variables to their default state prior to initialization, and any other necessary measures to put the class in the same state as if it had just been loaded.
Also in the preferred embodiment, the first and second applications run in a middleware environment. A middleware program is responsible for launching an application, for example to perform a transaction. This causes an application class loader instance to load the set of one or more classes for the first application. After this has completed, the middleware program initiates the step of resetting the application classes by calling a method of the application class loader. A reference to the application class loader instance is passed with this method, and the system then checks that this reference is the only valid remaining reference to the application class loader instance and the set of classes loaded by it. This ensures that the application class loader instance and its associated classes could actually be garbage collected now, apart from this one remaining reference. Then, once the necessary reinitialisation measures have been performed on the class loader instance and its classes, this reference is returned back to the middleware program, which can then start a new application using the refreshed class loader and classes. Note that this also avoids have to create a new instance of the application class loader itself.
In a more complicated implementation, the virtual machine maintains a pool of application class loader instances and associated classes. Each time the middleware program calls the reset method, the application class loader instances specified by the reference passed with this call are added to the pool. On return from the call, the middleware program receives a reference to a reinitialized application class loader instance and associated classes. This may be the same one as just surrendered, or it may be one surrendered by a previous reset call, and which is immediately available (i.e. has already been reinitialized). Note that if no suitable class loader instance is available from a pool, then the reset call will have to instantiate a new application class loader instance, which clearly will then need to load the application classes for itselfxe2x80x94this is effectively what happens in a conventional system without the ability to reuse as described herein.
Clearly many enhancements can be made to the operation of the pool, and the way in which the reset call interacts with it. For example parameters on the reset call might be used to indicate how long to wait for an reinitialized application class loader instance to become available before instantiating a new one. Such parameters might also be used to specify interest in only a subset of those class loaders in the pool, such as a particular subclass of the main application class loader, or perhaps requiring that the call return the particular class loader instance that is referenced in the call itself.
The invention further provides a computer system including a virtual machine supporting an object-oriented environment, in which programs to run on the virtual machine are formed from classes loaded into the virtual machine by a class loader, wherein a class must be initialized before being used by a program, said system comprising:
means for starting a first application on the virtual machine, including loading and initializing a set of one or more classes for said first application;
means for running said first application;
means responsive to the first application finishing running, for resetting at least one class from said set of one or more classes from the first application; and
means for starting a second application on the virtual machine, said second application initializing said at least one class that has been reset from the first application, prior to use of said at least one class by the second application.
The invention further provides a computer program product, comprising computer program instructions typically recorded onto a storage medium or transmitted over a network, for implementing the above methods.